#!/usr/bin/env python
#coding=utf-8 

# property 允许的属性值 property(fget = None, fset = None, fdel = None, doc = None)

class ProtectAndHideX(object):
	def __init__(self, x):
		assert isinstance(x, int), \
		'"x" must be an integer!'
		self.__x = ~x
	def get_x(self):
		return ~self.__x

	x = property(get_x)

inst = ProtectAndHideX(10)
print 'inst.x = ', inst.x
# inst.x = 20  会报错  不能设置


class HideX(object):
	def __init__(self, x):
		print "== __init__ =="
		self.x = x
	def get_x(self):
		print "== get_x =="
		return ~self.__x
	def set_x(self, x):
		assert isinstance(x, int), \
		'"x" must be an integer!'
		print "== set_x =="
		self.__x = ~x
	x = property(get_x, set_x) #
inst = HideX(20) # 也会调用set_x
print inst.x
inst.x = 30
print inst.x

class HideXX(object):
	def __init__(self, x):
		self.x = x
	@property
	def x(self):
		def fget(self):
			return ~self.__x
		def fset(self, x):
			assert isinstance(x, int), \
				'"x" must be an integer!'
			self.__x = ~x
		return locals() 

inst = HideXX(30)  # AttributeError: can't set attribute 似乎属性是只度属性  报错～～

print inst.x
inst.x = 30
print inst.x
	

